import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models

class ClassHyModel:
    def __init__(self):
        self.SecretId = "AKID64pMfkaCfuxm5PYTBgy2ueBTWWJtEMFu"
        self.SecretKey = "UjjV2NUN2SdcOzMVQ7ZnpdDbnmZ7TdYH"

        self.cred = credential.Credential(self.SecretId, self.SecretKey)

        self.httpProfile = HttpProfile()
        self.httpProfile.endpoint = "hunyuan.tencentcloudapi.com"

        # 实例化一个client选项，可选的，没有特殊需求可以跳过
        self.clientProfile = ClientProfile()
        self.clientProfile.httpProfile = self.httpProfile
        # 实例化要请求产品的client对象,clientProfile是可选的
        self.client = hunyuan_client.HunyuanClient(self.cred, "ap-beijing", self.clientProfile)

        # 实例化一个请求对象,每个接口都会对应一个request对象
        self.req = models.ChatCompletionsRequest()
        self.system_message = """你是一个微表情分析专家，如果用户没有提供数据，则无需按照下述要求进行分析.如果用户提供数据，需要根据提供的时序数据完成以下任务。：

                    1. 情绪分布分析：
                    - 统计各情绪出现频次及占比
                    - 识别主导情绪及其持续时间
                    - 标注情绪切换关键时间点（精确到秒）

                    2. 异常值检测：
                    - 发现频次过高的潜在误判情绪
                    - 标注反常情绪出现的时间点（如happiness的突然出现）
                    - 识别连续相同情绪的持续时间是否合理

                    3. 动态模式识别：
                    - 分析情绪转换模式（如disgust -> surprise的转换频率）
                    - 检测情绪脉冲现象（短时剧烈变化）
                    - 标注持续3秒以上的稳定情绪区间

                    请用以下格式结构化输出：
                    【主要发现】<关键结论>
                    【时间线】<时间范围及阶段划分>
                    【建议】<改进建议>
                    """
        self.Message = [
                {
                    "Role": "system",
                    "Content": self.system_message
                }
        ]
        self.params = {
            "Model": "hunyuan-large",
            "Messages": self.Message
        }
    #第一次返回的结果
    def first_chat(self,result,occur_time):
        user_question = f"""情绪数据集：{result},每一帧情绪识别的时间：{occur_time}"""
        user_message = {
            "Role": "user",
            "Content": user_question
        }
        self.Message.append(user_message)
        self.req.from_json_string(json.dumps(self.params))
        resp = self.client.ChatCompletions(self.req)
        answer = resp.Choices[0].Message.Content
        answer_messahge = {
            "Role": "assistant",
            "Content": answer
        }
        self.Message.append(answer_messahge)
        return answer


    def del_chat_message(self):
        if len(self.Message) >= 11 :
            del self.Message[3:8]

    def chat(self,question):
        user_question = question
        user_message = {
            "Role": "user",
            "Content": user_question
        }
        self.Message.append(user_message)
        self.params["Messages"] = self.Message
        print(self.Message)
        print(self.params)

        self.req.from_json_string(json.dumps(self.params))
        resp = self.client.ChatCompletions(self.req)
        answer = resp.Choices[0].Message.Content
        answer_message = {
            "Role": "assistant",
            "Content": answer
        }
        self.Message.append(answer_message)
        #删除多余上下文信息，使大模型反应更快
        #self.del_chat_message()
        return answer


# if __name__ == '__main__':
#     hy_model = HyModel()
#     occur_time = "[None, '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:31', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:32', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:33', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:34', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:35', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:36', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:37', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:38', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:39', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:40', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:41', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:42', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:43', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44', '15:43:44']"
#     result = "[None, 'disgust', 'surprise', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'sadness', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'sadness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'happiness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'sadness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'sadness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'happiness', 'happiness', 'happiness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'sadness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'sadness', 'sadness', 'disgust', 'happiness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'sadness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'happiness', 'disgust', 'disgust', 'disgust', 'disgust', 'happiness', 'disgust', 'happiness', 'disgust', 'happiness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'sadness', 'surprise', 'surprise', 'happiness', 'sadness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'sadness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'sadness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'sadness', 'disgust', 'disgust', 'disgust', 'surprise', 'happiness', 'repression', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'sadness', 'surprise', 'surprise', 'disgust', 'disgust', 'sadness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'sadness', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'sadness', 'disgust', 'disgust', 'disgust', 'happiness', 'disgust', 'disgust', 'disgust', 'surprise', 'sadness', 'disgust', 'surprise', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'disgust', 'happiness', 'sadness', 'disgust', 'happiness']"
#     answer = hy_model.first_chat(result=result , occur_time=occur_time)
#     print(answer)